package top.lyc829.mymusic.ext

import android.annotation.SuppressLint
import android.app.Activity
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.xuexiang.xui.utils.WidgetUtils
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog


//loading框
@SuppressLint("StaticFieldLeak")
private var loadingDialog : MiniLoadingDialog? = null

/**
 * 打开等待框
 */
fun AppCompatActivity.showLoading(message: String = "请求网络中") {
    if (!this.isFinishing) {
        if (loadingDialog == null) {
            loadingDialog = MiniLoadingDialog(this)

            loadingDialog =  WidgetUtils.getMiniLoadingDialog(this)
            loadingDialog?.updateMessage(message)
            loadingDialog?.setCancelable(false)
            loadingDialog?.show()
        }
        if (loadingDialog?.isLoading == true) {
            loadingDialog?.dismiss()
        }
        loadingDialog?.show()
    }
}

/**
 * 打开等待框
 */
fun Fragment.showLoading(message: String = "请求网络中") {
    activity?.let {
        if (!it.isFinishing) {
            if (loadingDialog == null) {
                loadingDialog = MiniLoadingDialog(it)

                loadingDialog =  WidgetUtils.getMiniLoadingDialog(it)
                loadingDialog?.updateMessage(message)
                loadingDialog?.setCancelable(false)
                loadingDialog?.show()
            }
            if (loadingDialog?.isLoading == true) {
                loadingDialog?.dismiss()
            }
            loadingDialog?.show()
        }
    }
}

/**
 * 关闭等待框
 */
fun Activity.dismissLoading() {
    loadingDialog?.dismiss()
}

fun Activity.destroyLoading() {
    dismissLoading()
    loadingDialog = null
}

fun Fragment.dismissLoading() {
    loadingDialog?.dismiss()
}

fun Fragment.destroyLoading() {
    dismissLoading()
    loadingDialog = null
}
